﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FreedomERP.Models;
using FreedomERP.Comm;
using FreedomERP.Models.Sd;
using FreedomERP.Dao.Sd;
using FreedomERP.Dao.Sd.Impl;
using FreedomERP.Comm.Utils;
using FreedomERP.Dao.Bd.Impl;
using FreedomERP.Dao.Bd;

namespace FreedomERP.Services.Sd.Impl
{
    public class SdSaleOrderServiceImpl : ISdSaleOrderService
    {
        ISdSaleOrderDAO saleOrderDAO = new SdSaleOrderDAOImpl();
        IBdCategoryDao categoryDao = new BdCategoryDaoImpl();
        ISdSaleOrderDetailDAO saleOrderDetailDAO = new SdSaleOrderDetailDAOImpl();

        /// <summary>
        /// 根据条件获取列表
        /// </summary>
        /// <author>梁建东</author>
        /// <returns></returns>
        public List<SdSaleOrder> QuerySaleOrderPaggingByParam(PagingParams paggingParams, out int totalCount) 
        {
            return saleOrderDAO.QuerySaleOrderPaggingByParam(paggingParams, out totalCount);
        }

        /// <summary>
        /// 根据FId获取订单信息
        /// </summary>
        /// <author>梁建东</author>
        /// <returns></returns>
        public SdSaleOrder GetSaleOrderByFId(String FId)
        {
            return saleOrderDAO.GetSaleOrderByFId(FId);
        }

        /// <summary>
        /// 添加订单
        /// </summary>
        /// <author>梁建东</author>
        /// <returns></returns>
        public void AddSaleOrder(SdSaleOrderView saleOrderView, string currAccountFId,string currOrgunitFId, out string FId)
        {
            SdSaleOrder saleOrder = new SdSaleOrder();
            saleOrder.FId = GuidUtils.getGuid();
            saleOrder.FNumber = ConstData.SALE_ORDER_NUMBER_HEAD + DateUtils.GetCurDateFormat("yyyyMMddHHmmss");
            saleOrder.FName = saleOrder.FNumber;
            saleOrder.FBillDate = DateUtils.ParseStringToDateTime(saleOrderView.FBillDate);
            saleOrder.FBusinessCategoryId = saleOrderView.FBusinessCategoryId;
            saleOrder.FCustomerName = saleOrderView.FCustomerName;
            saleOrder.FCustomerPhone = saleOrderView.FCustomerPhone;
            saleOrder.FCustomerMobile = saleOrderView.FCustomerMobile;
            saleOrder.FCustomerAddress = saleOrderView.FCustomerAddress;
            saleOrder.FCreateTime = DateUtils.GetCurDate();
            saleOrder.FCreatorId = currAccountFId;
            saleOrder.FModifyTime = DateUtils.GetCurDate();
            saleOrder.FModifierId = currAccountFId;
            saleOrder.FRemark = saleOrderView.FRemark;
            saleOrder.FOrgunitId = currOrgunitFId;
            saleOrder.FStatusId = categoryDao.QueryCategoryFIdByFTypeAndFNumber(ConstData.CATEGORY_COMM_ORDER_STATUS, ConstData.COMM_ORDER_STATUS_SAVE);

            if (saleOrderView.FDetails != null)
            {
                foreach (SdSaleOrderDetailView detailView in saleOrderView.FDetails)
                {
                    SdSaleOrderDetail detail = new SdSaleOrderDetail();
                    detail.FId = GuidUtils.getGuid();
                    detail.FBarcode = detailView.FBarcode;

                    saleOrder.SdSaleOrderDetail.Add(detail);
                }
            }

            saleOrderDAO.AddSaleOrder(saleOrder);
            saleOrderDAO.Save();

            FId = saleOrder.FId;
        }

        /// <summary>
        /// 修改订单
        /// </summary>
        /// <author>梁建东</author>
        /// <returns></returns>
        public void ModifySaleOrder(SdSaleOrderView saleOrderView, string currAccountFId)
        {
            SdSaleOrder saleOrder = saleOrderDAO.GetSaleOrderByFId(saleOrderView.FId);

            if (saleOrder.BdCategory1 != null && saleOrder.BdCategory1.FNumber.Equals(ConstData.COMM_ORDER_STATUS_APPROVE))
            {
                throw new Exception(ConstNoteMessage.SALE_ORDER_MODIFY_ERROR);
            }

            if (saleOrder != null)
            {
                saleOrder.FNumber = saleOrderView.FNumber;
                saleOrder.FName = saleOrderView.FNumber;
                saleOrder.FBillDate = DateUtils.ParseStringToDateTime(saleOrderView.FBillDate);
                saleOrder.FBusinessCategoryId = saleOrderView.FBusinessCategoryId;
                saleOrder.FCustomerName = saleOrderView.FCustomerName;
                saleOrder.FCustomerPhone = saleOrderView.FCustomerPhone;
                saleOrder.FCustomerMobile = saleOrderView.FCustomerMobile;
                saleOrder.FCustomerAddress = saleOrderView.FCustomerAddress;
                saleOrder.FModifyTime = DateUtils.GetCurDate();
                saleOrder.FModifierId = currAccountFId;
                saleOrder.FRemark = saleOrderView.FRemark;

                //判断是否删除明细
                List<SdSaleOrderDetail> deleteList = new List<SdSaleOrderDetail>();
                if (saleOrder.SdSaleOrderDetail != null)
                {
                    foreach (SdSaleOrderDetail detail in saleOrder.SdSaleOrderDetail)
                    {
                        if (saleOrderView.FDetails != null)
                        {
                            bool flag = false;
                            foreach (SdSaleOrderDetailView detailView in saleOrderView.FDetails)
                            {
                                if (detail.FId.Equals(detailView.FId))
                                {
                                    flag = true;
                                    break;
                                }
                            }
                            if (!flag)
                            {
                                deleteList.Add(detail);
                            }
                        }
                    }
                }

                //添加或修改明细
                if (saleOrderView.FDetails != null)
                {
                    foreach (SdSaleOrderDetailView detailView in saleOrderView.FDetails)
                    {
                        if (saleOrder.SdSaleOrderDetail != null && detailView.FId != null && !detailView.FId.Trim().Equals("") && !detailView.FId.Trim().Equals("-1"))
                        {
                            var query = (from d in saleOrder.SdSaleOrderDetail where d.FId == detailView.FId select d).ToList<SdSaleOrderDetail>();
                            if (query != null && query.Count > 0)
                            {
                                SdSaleOrderDetail detailOld = query.First<SdSaleOrderDetail>();
                                detailOld.FBarcode = detailView.FBarcode;
                            }
                            else
                            {
                                SdSaleOrderDetail detail = new SdSaleOrderDetail();
                                detail.FId = GuidUtils.getGuid();
                                detail.FBarcode = detailView.FBarcode;

                                saleOrder.SdSaleOrderDetail.Add(detail);
                            }
                        }
                        else
                        {
                            SdSaleOrderDetail detail = new SdSaleOrderDetail();
                            detail.FId = GuidUtils.getGuid();
                            detail.FBarcode = detailView.FBarcode;

                            saleOrder.SdSaleOrderDetail.Add(detail);
                        }
                    }
                }

                //删除明细
                foreach (SdSaleOrderDetail objDelete in deleteList)
                {
                    saleOrder.SdSaleOrderDetail.Remove(objDelete);
                }

                saleOrderDAO.Save();
            }
        }

        /// <summary>
        /// 审核订单
        /// </summary>
        /// <author>梁建东</author>
        /// <returns></returns>
        public void ApproveSaleOrder(string FId, string currAccountFId)
        {
            SdSaleOrder saleOrder = saleOrderDAO.GetSaleOrderByFId(FId);

            if (saleOrder.BdCategory1 != null && saleOrder.BdCategory1.FNumber.Equals(ConstData.COMM_ORDER_STATUS_APPROVE))
            {
                throw new Exception(ConstNoteMessage.SALE_ORDER_APPROVE_ERROR);
            }

            if (saleOrder != null)
            {
                saleOrder.FStatusId = categoryDao.QueryCategoryFIdByFTypeAndFNumber(ConstData.CATEGORY_COMM_ORDER_STATUS, ConstData.COMM_ORDER_STATUS_APPROVE);
                saleOrder.FApproveTime = DateUtils.GetCurDate();
                saleOrder.FApproverId = currAccountFId;
                saleOrderDAO.Save();
            }
        }

        /// <summary>
        /// 反审核订单
        /// </summary>
        /// <author>梁建东</author>
        /// <returns></returns>
        public void UnApproveSaleOrder(string FId, string currAccountFId)
        {
            SdSaleOrder saleOrder = saleOrderDAO.GetSaleOrderByFId(FId);

            if (saleOrder.BdCategory1 != null && !saleOrder.BdCategory1.FNumber.Equals(ConstData.COMM_ORDER_STATUS_APPROVE))
            {
                throw new Exception(ConstNoteMessage.RECEIVE_ORDER_UNAPPROVE_ERROR);
            }

            if (saleOrder != null)
            {
                saleOrder.FStatusId = categoryDao.QueryCategoryFIdByFTypeAndFNumber(ConstData.CATEGORY_COMM_ORDER_STATUS, ConstData.COMM_ORDER_STATUS_SAVE);
                saleOrder.FApproveTime = null;
                saleOrder.FApproverId = null;
                saleOrderDAO.Save();
            }
        }

        /// <summary>
        /// 获取订单明细
        /// </summary>
        /// <author>梁建东</author>
        /// <returns></returns>
        public List<SdSaleOrderDetail> GetSaleOrderDetailListByFParentId(string FParentId)
        {
            return saleOrderDetailDAO.GetSaleOrderDetailListByFParentId(FParentId);
        }
    }
}